package com.example.demo.service;

import com.example.demo.vo.ProductVO;
import com.example.demo.mapper.ProductMapper;
import com.example.demo.model.ProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
// 实现实现 ProductService 接口
public class ProductService implements ProductMapper {

    // 注入ProductMapper实例
    @Autowired
    private ProductMapper productMapper;

    // 实现 ProductService 接口方法
    public List<ProductInfo> productChoose(String brand, int minPrice,int maxPrice) {
        // 使用注入的ProductMapper实例调用方法
        List<ProductInfo> products = productMapper.productChoose(brand, minPrice, maxPrice);
        return products;
    }

    public List<ProductInfo> getAllProducts() {
        return productMapper.getAllProducts();
    }


    // 重命名方法以避免冲突
    @Transactional
    public boolean createProduct(ProductInfo product) {
        try {
            // 设置默认值
            product.setStatus(1); // 1-正常在售
            product.setCreateTime(new Date());
            product.setUpdateTime(new Date());
            product.setViewCount(0);
            product.setFavoriteCount(0);
            
            return productMapper.insertProduct(product) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 实现接口方法
    @Override
    public int insertProduct(ProductInfo product) {
        return productMapper.insertProduct(product);
    }

    public List<ProductVO> getProductsByViewCount(int limit) {
        return productMapper.getProductsByViewCount(limit);
    }

    @Override
    public List<ProductVO> getProductDetail(int id) {
        return productMapper.getProductDetail(id);
    }

    @Override
    public void incrementViewCount(int id) {
        productMapper.incrementViewCount(id);
    }

    public List<ProductInfo> filterProducts(Map<String, Object> params) {
        return productMapper.filterProducts(params);
    }

    @Override
    public List<ProductVO> getProductsByCategorySearch(Map<String, Object> params) {
        return List.of();
    }

    public ProductInfo getById(Integer id) {
        return productMapper.getById(id);
    }
}


